package com.beordie.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Classname DBUtils
 * @Description 数据库连接工具
 * @Date 2021/7/11 18:41
 * @Created 30500
 */
public class DBUtils {
    private static String url;
    private static String username;
    private static String password;
    private static String driverName;
    // 插入时接收位置ＩＤ
    private Integer count;
    private Connection connection;
    protected PreparedStatement statement;
    protected ResultSet resultSet;
    private static DruidDataSource source = new DruidDataSource();

    public PreparedStatement getStatement() {
        return statement;
    }

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        url = bundle.getString("url");
        username = bundle.getString("username");
        password = bundle.getString("password");
        driverName = bundle.getString("driverClassName");

        source.setUsername(username);
        source.setPassword(password);
        source.setUrl(url);
        source.setDriverClassName(driverName);
    }

    /**
     * @description 拿到数据库的连接对象
     * @author 30500
     * @date 2021/7/11 20:07
     * @type []
     * @return java.sql.Connection
     */
    public Connection getConnection() {
        try {
            this.connection = source.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return this.connection;
    }

    /**
     * @description 拿到语句预处理对象
     * @author 30500
     * @date 2021/7/11 20:08
     * @type [java.lang.String]
     * @return java.sql.PreparedStatement
     */
    public PreparedStatement getStatement(String sql) {
        try {
            this.statement = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return this.statement;
    }

    /**
     * @description 设置参数
     * @author 30500
     * @date 2021/7/11 20:16
     * @type [java.util.List<java.lang.Object>]
     * @return void
     */
    public void setParams(List<Object> params) {
        if (params == null || params.size() == 0) {
            return;
        }
        for (int i = 0; i < params.size(); i++) {
            try {
                this.statement.setObject(i + 1, params.get(i));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * @description 数据库的增、删、改操作
     * @author 30500
     * @date 2021/7/11 20:19
     * @type [java.lang.String, java.util.List<java.lang.Object>]
     * @return int
     */
    public int update(String sql, List params) {
        getStatement(sql);
        setParams(params);
        try {
            this.count = this.statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return this.count;
    }

    /**
     * @description 数据库的查询操作
     * @author 30500
     * @date 2021/7/11 20:20
     * @type [java.lang.String, java.util.List<java.lang.Object>]
     * @return java.sql.ResultSet
     */
    public ResultSet query(String sql, List params) {
        getStatement(sql);
        setParams(params);
        try {
            this.resultSet = this.statement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return this.resultSet;
    }

    /**
     * @description 关闭所有的连接对象
     * @author 30500
     * @date 2021/7/11 20:21
     * @type []
     * @return void
     */
    public void closeAll() {
        try {
            if (this.resultSet != null)
                this.resultSet.close();
            if (this.statement != null)
                this.statement.close();
            if (this.connection != null)
                this.connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
